package spe.ui.action;

import java.util.List;

import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.ui.IWorkbenchPart;

import spe.ui.editor.part.AbstractElementNodeEditPart;
import spe.ui.editor.part.GraphEditPart;

public class PropertiesSelectionAction extends SelectionAction 
{
	public static final String ID = PropertiesSelectionAction.class.getName();
	public static final String REQ_TYPE = "SHOWPROPERTIES";
	
	private Request request;
	
	public PropertiesSelectionAction(IWorkbenchPart part) 
	{
		super(part);
		
		setText("Properties");
		request = new Request(REQ_TYPE);
	}

	@Override
	protected boolean calculateEnabled()
	{
		if( (getSelectedObjects().size() == 1) && !(getSelectedObjects().get(0) instanceof GraphEditPart) )
			return true;
		
		return false;
	}
	
	
	@Override
	public void run()
	{
		@SuppressWarnings("unchecked")
		List<AbstractElementNodeEditPart> parts = getSelectedObjects();
		
		CompoundCommand compoundCmd = new CompoundCommand();
		//System.out.println(String.format("Size of selected object list: %d, the first object: %s", getSelectedObjects().size(), getSelectedObjects().get(0)));
		for(AbstractElementNodeEditPart p: parts)
		{
			Command cmd = p.getCommand(request);
			compoundCmd.add(cmd);
			System.out.println(String.format("[PropertiesSelectionAction-RUN] Part %s, request %s", cmd, request));
		}
		execute(compoundCmd);
	}
	
	public String getId()
	{
		return ID;
	}

}
